//
//  MDataProvider.m
//  ShopStore
//
//  Created by 李超 on 2017/04/28.
//  Copyright © 2017年 dongheng. All rights reserved.
//

#import "MDataProvider.h"
#import "ListModel.h"
#import "ActivityModel.h"
#import "PropertyListModel.h"
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "MessageModel.h"
@implementation MDataProvider
//登录
+(void)login:(NSMutableDictionary *)dic sucess:(void(^)(NSDictionary *dic))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appCompanyAppUser/getloginInfo";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
    {
        NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
        NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
        if ([headDic[@"status"] integerValue] == 0)
        {
            block(bodyDic);
        }
        else
        {
            faile(headDic[@"msg"]);
        }
    } failure:^(NSError *error) {
        faile(NETWORKERROR);
    }];
}
//修改密码
+(void)modifyPassword:(NSMutableDictionary *)dic sucess:(void(^)(NSDictionary *resultDic))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appCompanyAppUser/savePass";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
//         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             //将新密码存到本地
             set_sp(@"app_pass", [data[@"packet"] objectForKey:@"new_pass"]);
             block(headDic);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
          faile(NETWORKERROR);
     }];
}
//带图片的信息列表-首页
+(void)homeList:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appInfo/getPicInfoList";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSArray *arr = [ListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"infoList"]];
             block(arr);
         }
         else
         {
              faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//带图片的信息列表加分页-政策服务
+(void)getPhotoListBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appInfo/getPicInfoListByPage";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSArray *arr = [ListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"infoList"]];
             block(arr);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//不带图片的信息列表加分页-党建栏目
+(void)getListWithBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appInfo/getPicInfoListByPage";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSArray *arr = [ListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"infoList"]];
             block(arr);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
       faile(NETWORKERROR);
     }];
}
//社工服务
+(void)socialWorkerServiceWithBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appInfo/getAllInfoByPage";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSArray *arr = [ListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"infoList"]];
             block(arr);
         }
         else
         {
            faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//物业服务
+(void)getPropertyListWithBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appInfo/getAllInfoByPage";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSArray *arr = [ListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"infoList"]];
             block(arr);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
       faile(NETWORKERROR);
     }];
}
//信息详情
+(void)messageDetailBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSMutableDictionary *detailModel))block faile:(void(^)(NSString *str))faile
{
    [SVProgressHUD show];
    NSString *str = @"/appInfo/getInfo";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         [SVProgressHUD dismiss];
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             
             NSMutableDictionary *resultDic = [[NSMutableDictionary alloc]initWithCapacity:0];
             MessageDetailModel *infoModel = [MessageDetailModel mj_objectWithKeyValues:bodyDic[@"info"]];
             
              NSArray *listArr = [TagListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"tagList"]];
             [resultDic setObject:infoModel forKey:@"info"];
             [resultDic setObject:listArr forKey:@"tagList"];
             block(resultDic);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
        faile(NETWORKERROR);
     }];
}
//企业信息展示
+(void)getEnterpriseMessageBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(EnterpriseModel *detailModel))block faile:(void(^)(NSString *str))faile
{
    [SVProgressHUD show];
    NSString *str = @"/appCompany/getCompanyForApp";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         [SVProgressHUD dismiss];
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             EnterpriseModel *model = [EnterpriseModel mj_objectWithKeyValues:bodyDic[@"company"]];
             block(model);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//活动列表
+(void)activityListBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appEvent/getEventList";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSArray *arr = [ActivityModel mj_objectArrayWithKeyValuesArray:bodyDic[@"eventList"]];
             block(arr);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//活动详情
+(void)activityDetailBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(ActivityDetailModel *model))block faile:(void(^)(NSString *str))faile
{
    [SVProgressHUD show];
    NSString *str = @"/appEvent/getEventInfo";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         [SVProgressHUD dismiss];
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             ActivityDetailModel *model = [ActivityDetailModel mj_objectWithKeyValues:bodyDic[@"event"]];
             block(model);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//活动报名
+(void)activitySignupBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSString *resultStr))block faile:(void(^)(NSString *str))faile
{
    NSString *str = @"/appEvent/saveSignUp";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
//         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             block(headDic[@"msg"]);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//获取社工服务列表
+(void)socialWorkerListBodyDic:(NSMutableDictionary *)dic sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    [SVProgressHUD show];
    NSString *str = @"/appProperty/getPropertyList";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         [SVProgressHUD dismiss];
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
              NSArray *arr = [PropertyListModel mj_objectArrayWithKeyValuesArray:bodyDic[@"propertyList"]];
             block(arr);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//获取留言列表
+(void)getMessageListBodyDic:(NSMutableDictionary *)dic typeStr:(NSInteger)type sucess:(void(^)(NSArray *array))block faile:(void(^)(NSString *str))faile
{
    NSString *str;
    switch (type)
    {
        case 0:
            //留言列表
            str = @"/appMessage/getMessagePageList";
            break;
        case 1:
            //保修列表
            str = @"/appRepair/getRepairPageList";
            break;
        default:
            break;
    }
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    NSString *str1 = [self dictionaryToJson:[self dicWithBodyDic:dic]];
    NSData* body = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [self postWithUrl:url body:body success:^(__autoreleasing id data)
     {
         NSDictionary *bodyDic =[data[@"packet"] objectForKey:@"body"];
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
         if ([headDic[@"status"] integerValue] == 0)
         {
             NSMutableArray *arr = [[NSMutableArray alloc]initWithCapacity:0];;
             if (type == 0)
             {
                 NSArray *resultArr = [NSArray arrayWithArray:bodyDic[@"messageList"]];
                 for (NSDictionary *dic in resultArr)
                 {
                     MessageModel *model = [[MessageModel alloc]initWithType:0 dictionary:dic];
                     [arr addObject:model];
                 }
             }
             else
             {
                 NSArray *resultArr = [NSArray arrayWithArray:bodyDic[@"repairList"]];
                 for (NSDictionary *dic in resultArr)
                 {
                     MessageModel *model = [[MessageModel alloc]initWithType:1 dictionary:dic];
                      [arr addObject:model];
                 }
             }
             block(arr);
         }
         else
         {
             faile(headDic[@"msg"]);
         }
     } failure:^(NSError *error) {
         faile(NETWORKERROR);
     }];
}
//留言建议
+(void)leaveMessageBodyDic:(CommitModel *)commitModel sucess:(void(^)(NSString *success))block faile:(void(^)(NSString *str))faile
{
    NSMutableDictionary  *dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    NSString *str = @"/appMessage/saveMessage";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    
    NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSDate *date = [NSDate date];
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600*8];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [formatter stringFromDate:date];
    [dic setObject:uuid forKey:@"uuid"];
    NSString *app_user =  get_sp(@"app_user");
    if (!app_user)
    {
        app_user = @"";
    }
    NSString *app_pass =  get_sp(@"app_pass");
    if (!app_pass)
    {
        app_pass = @"";
    }
    [dic setObject:app_user forKey:@"app_user"];
    [dic setObject:app_pass forKey:@"app_pass"];
    [dic setObject:dateStr forKey:@"requestData"];
    [dic setObject:@"1" forKey:@"version"];
    [dic setObject:commitModel.name forKey:@"messager"];
    [dic setObject:commitModel.phone forKey:@"messager_phone"];
    [dic setObject:commitModel.file_function forKey:@"file_function"];
    [dic setObject:commitModel.message_content forKey:@"message_content"];
    [dic setObject:get_sp(@"company_id") forKey:@"company_id"];
    [dic setObject:commitModel.message_obj forKey:@"message_obj"];
    [dic setObject:commitModel.messageobjidstr forKey:@"messageobjidstr"];
//    NSMutableArray *imageParameters = [NSMutableArray arrayWithCapacity:0];
//    for (int i = 0; i < commitModel.imgArr.count; i++)
//    {
//        NSInteger count = arc4random()%10;
//        UIImage *messageImg = commitModel.imgArr[i];
//        NSString *imgName = [NSString stringWithFormat:@"IMG_%@%ld12345.jpg",dateStr1,count];
//        [imageParameters addObject:@{@"name": imgName, @"value": [self compressImageSize:messageImg toByte:3072]}];
//    }
    [self duPostWithUrl:url commitModel:commitModel Parameters:dic Block:^(id data)
    {
        NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
        if ([headDic[@"status"] integerValue]==0)
        {
            block(headDic[@"msg"]);
        }
        else
        {
            faile(headDic[@"msg"]);
        }
    }];
}
//保修
+(void)warrantyBodyDic:(CommitModel *)commitModel sucess:(void(^)(NSString *success))block faile:(void(^)(NSString *str))faile
{
    NSMutableDictionary  *dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    NSString *str = @"/appRepair/saveRepair";
    NSString  *url = [NSString stringWithFormat:@"%@%@",ENCLOUD_URL,str];
    
    NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSDate *date = [NSDate date];
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600*8];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [formatter stringFromDate:date];
   
    [dic setObject:uuid forKey:@"uuid"];
    NSString *app_user =  get_sp(@"app_user");
    if (!app_user)
    {
        app_user = @"";
    }
    NSString *app_pass =  get_sp(@"app_pass");
    if (!app_pass)
    {
        app_pass = @"";
    }
    [dic setObject:app_user forKey:@"app_user"];
    [dic setObject:app_pass forKey:@"app_pass"];
    [dic setObject:dateStr forKey:@"requestData"];
    [dic setObject:@"1" forKey:@"version"];
    
    [dic setObject:commitModel.name forKey:@"repair_man"];
    [dic setObject:commitModel.phone forKey:@"repair_phone"];
    [dic setObject:commitModel.file_function forKey:@"file_function"];
    [dic setObject:commitModel.message_content forKey:@"repair_content"];
    [dic setObject:get_sp(@"company_id") forKey:@"company_id"];
//    [dic setObject:commitModel.message_obj forKey:@"message_obj"];
    [dic setObject:commitModel.messageobjidstr forKey:@"property_id"];
    [SVProgressHUD showWithStatus:nil];
    [self duPostWithUrl:url commitModel:commitModel Parameters:dic Block:^(id data)
    {
         NSDictionary *headDic = [data[@"packet"] objectForKey:@"head"];
        if ([headDic[@"status"] integerValue]==0)
        {
            block(headDic[@"msg"]);
        }
        else
        {
            faile(headDic[@"msg"]);
        }
    }];
}
#pragma mark ---Http---NilPost---请求
+(void)duPostNilWithUrl:(NSString*)url Parameters:(NSMutableDictionary*)parameters Block:(void (^)(id data))block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        block(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"加载失败"];
        NSLog(@"error--%@",error);
    }];
}
#pragma mark ---Http---Post---请求
+(void)postWithUrl:(NSString *)url body:(NSData *)body  success:(void(^)(id data))success failure:(void(^)(NSError *error))failure
{
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:nil error:nil];
//    request.timeoutInterval= TIME_OUT_INTERVAL;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    // 设置body
    [request setHTTPBody:body];
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",@"text/plain",nil];
    manager.responseSerializer = responseSerializer;
    [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error)
        {
            NSString *resString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSData* data = [resString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *resultDict = [NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];
            success(resultDict);
        }
        else
        {
            failure(error);
        }
    }] resume];
}
#pragma mark ---Http---Post---带上传图片的
+(void)duPostWithUrl:(NSString*)url commitModel:(CommitModel *)model Parameters:(NSMutableDictionary*)parameters Block:(void (^)(id data))block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];//设置服务器允许的请求格式内容
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"multipart/form-data", @"application/json", @"text/html", @"image/jpeg", @"image/png", @"application/octet-stream", @"text/json", nil];
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id  _Nonnull formData)
    {
        if(model.imgArr.count > 0||model.videoArr.count >0||model.audeoArr.count > 0)
        {
            if (model.imgArr.count > 0)
            {
                //图片
                NSInteger i = 0;
                for (UIImage *image in model.imgArr)
                {
                    UIImage *dataImg = [self compressImageSize:image toByte:3072];
                    [formData appendPartWithFileData:UIImageJPEGRepresentation(dataImg, 1.0) name:@"uploadFile"/*[parameter objectForKey:@"name"]*/ fileName:[NSString stringWithFormat:@"photo_%ld.png", i] mimeType:@"image/jpeg"];
                    i++;
                }
            }
            if (model.videoArr.count >0)
            {
                for (NSData *video in model.videoArr)
                {
                    NSInteger i = 0;
                    [formData appendPartWithFileData:video name:@"uploadFile" fileName:[NSString stringWithFormat:@"video%ld.mov",i] mimeType:@"video/quicktime"];
                    i++;
                }
            }
            if (model.audeoArr.count > 0)
            {
                //音频
                for (NSData *audio in model.audeoArr)
                {
                    [formData appendPartWithFileData:audio name:@"uploadFile" fileName:@"message.mp3" mimeType:@"amr/mp3/wmr"];
                }
            }
         }
        else
        {
             NSData *data = [[NSData alloc]init];
             [formData appendPartWithFileData:data name:@"uploadFile"fileName:[NSString stringWithFormat:@"photo.png"] mimeType:@"image/jpeg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]])
        {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            block(dic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"加载失败"];
        NSLog(@"error = %@",error);
    }];
}
#pragma mark ---字典转json---
+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
#pragma mark ---等比例压缩图片内存大小---
+ (UIImage *)compressImageSize:(UIImage *)image toByte:(NSUInteger)maxLength
{
    maxLength = maxLength * 1000;
    UIImage *resultImage = image;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),(NSUInteger)(resultImage.size.height * sqrtf(ratio)));
        UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, 1);
    }
    return resultImage;
}
/*NSDictionary *json = @{@"packet":@{@"head":@{@"uuid":@"768343198413",@"app_user":@"admin",@"app_pass":@"dd94709528bb1c83d08f3088d4043f4742891f4f",@"requestData":@"2017-07-1015: 03: 24.879",@"version": @"1" },@"body":@{@"app_user":@"admin",@"app_pass":@"dd94709528bb1c83d08f3088d4043f4742891f4f"}}};
*/
+(NSDictionary *)dicWithBodyDic:(NSMutableDictionary *)bodyDic
{
    NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSDate *date = [NSDate date];
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600*8];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    NSMutableDictionary *headDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [headDic setObject:uuid forKey:@"uuid"];
    NSString *app_user =  get_sp(@"app_user");
    if (!app_user)
    {
        app_user = @"";
    }
    NSString *app_pass =  get_sp(@"app_pass");
    if (!app_pass)
    {
        app_pass = @"";
    }
    [headDic setObject:app_user forKey:@"app_user"];
    [headDic setObject:app_pass forKey:@"app_pass"];
    
    [headDic setObject:dateStr forKey:@"requestData"];
     [headDic setObject:@"1" forKey:@"version"];
    [requestDic setObject:headDic forKey:@"head"];
    [requestDic setObject:bodyDic forKey:@"body"];
    [jsonDic setObject:requestDic forKey:@"packet"];
    return jsonDic;
}
@end
